package com.axzhengxin.platform.sync.impl;

import cn.hutool.db.sql.SqlExecutor;
import com.axzhengxin.platform.sync.ISyncDataService;
import com.ruoyi.common.utils.spring.SpringUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.Comparator;

/**
 * SyncBizDataServiceImpl
 *
 * @author jiangnan
 * @date 2021/4/8
 */
@Service("syncBizData")
public class SyncBizDataServiceImpl implements ISyncDataService {

    @Autowired
    private DataSource ecscDs;

    @SneakyThrows
    @Override
    public void exec() {
        try (Connection conn = ecscDs.getConnection()) {
            SqlExecutor.execute(conn, "truncate table biz_apply_info");
            SqlExecutor.execute(conn, "truncate table biz_approve_info");
            SqlExecutor.execute(conn, "truncate table biz_approve_detail");
            SqlExecutor.execute(conn, "truncate table biz_loan_info");
            SqlExecutor.execute(conn, "truncate table biz_flow_info");
            SqlExecutor.execute(conn, "truncate table biz_flow_task");
        }
        SpringUtils.getBeansOfType(ISyncDataService.class)
                .values()
                .stream()
                .filter(bean -> bean.getClass().getName().contains("Biz"))
                .filter(service -> !(service instanceof SyncBizDataServiceImpl))
                .sorted(Comparator.comparingInt(ISyncDataService::getOrder))
                .forEachOrdered(ISyncDataService::exec);
    }

    @Override
    public int getOrder() {
        return 0;
    }

}
